package com.xframe.xdal.core.handler;

import com.xframe.xdal.core.constant.DataSourceTypeEnums;
import com.xframe.xdal.core.handler.valueTake.IValueTakingStrategy;
import com.xframe.xdal.core.handler.valueTake.ValueTakingStrategyFactory;
import com.xframe.xdal.core.model.mapping.IColumnMapping;
import com.xframe.xdal.core.model.mapping.IRelation;
import com.xframe.xdal.core.model.mapping.ITableInfo;
import lombok.Data;
import lombok.extern.log4j.Log4j2;

/**
 * 功能描述: 基于JDBC的查询处理器，映射帮助对象
 *
 * @author xufeng
 * @version 1.0
 * @date 2023-03-20
 */
@Log4j2
@Data
public class HandlerColumn {

    /**
     * 处理的列号
     */
    private Integer colIndex;
    /**
     * 处理的别名
     */
    private String colAlName;
    /**
     * 引用属性路径
     */
    private String refPathName;

    /**
     * 对应的映射
     */
    private IColumnMapping columnMapping;

    /**
     * 是否一个引用
     */
    private boolean refFlag = false;
    /**
     * 是否一个列表
     */
    private boolean refListFlag = false;

    /**
     * 引用子表 或者引用列 的表结构对象
     */
    private ITableInfo refTableInfo = null;
    /**
     * 引用的数据类型
     */
    private Class<?> dataType;

    private IRelation relation;

    /**
     * 构造函数
     */
    public HandlerColumn(int colIndex,String refPathName,String colAlName,IColumnMapping columnMapping){
        this.colAlName = colAlName;
        this.refPathName = refPathName;
        this.colIndex = colIndex;
        this.columnMapping = columnMapping;
    }
    /**
     * 引用列 或者 引用子表构造函数
     */
    public HandlerColumn(int colIndex, String refPathName, String colAlName, ITableInfo tableInfo,IRelation relation,
                         IColumnMapping columnMapping, boolean refFlag, boolean refListFlag, Class<?> dataType){
        this.colAlName = colAlName;
        this.colIndex = colIndex;
        this.refPathName = refPathName;
        this.columnMapping = columnMapping;
        this.refFlag = refFlag;
        this.refListFlag = refListFlag;
        this.dataType = dataType;
        this.refTableInfo = tableInfo;
        this.relation = relation;
    }
    // 取值策略
    public IValueTakingStrategy getValueTakingStrategy(DataSourceTypeEnums dataSourceTypeEnums){
        IValueTakingStrategy valueTakingStrategy = ValueTakingStrategyFactory.create(this.columnMapping,dataSourceTypeEnums);
        return valueTakingStrategy;
    }
//
//    // 设置策略
//    public void setValueStrategy(Object bean,Object value){
//        if(!refFlag && !refListFlag){
//            XReflect.setFieldValue(bean,this.columnMapping.getFieldName(),value);
//        }
//        if(refFlag){
//            // 引用列
//            Object refObject = XReflect.getFieldValueByInit(bean,this.refPathName);
//            XReflect.setFieldValue(refObject,this.columnMapping.getFieldName(),value);
//        }
//        if(refListFlag){
//            // 子列表处理
//            //Field field = XReflect.getAccessibleField(bean, this.refPathName);
//            Object refListObject = XReflect.getFieldValueByInit(bean,this.refPathName);
//            log.info(refListObject);
//        }
//    }


    @Override
    public boolean equals(Object anObject) {
        if (anObject instanceof HandlerColumn){
            HandlerColumn handlerColumn = (HandlerColumn)anObject;
            return this.colIndex.equals(handlerColumn.colIndex);
        }else {
            return false;
        }
    }

    @Override
    public int hashCode(){
        return this.colIndex.hashCode();
    }



}
